package informationextraction;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import xmlcommandset.UsePermission;
import xmlparser.XMLInformationListener;
import xmlparser.XMLParser.ContentContext;
import xmlparser.XMLParser.ElementContext;

public class UsePermissionExtraction {
	XMLInformationListener xml_info = new XMLInformationListener();
	List<UsePermission> list_use_permission = new ArrayList<UsePermission>();

	public UsePermissionExtraction(XMLInformationListener xml_info) {
		this.xml_info = xml_info;
	}

	public XMLInformationListener getXml_info() {
		return xml_info;
	}

	public void setXml_info(XMLInformationListener xml_info) {
		this.xml_info = xml_info;
	}

	public List<UsePermission> getListUsePermission() {
		return list_use_permission;
	}

	public void setUse_permission(List<UsePermission> use_permission) {
		this.list_use_permission = use_permission;
	}

	public void setUsePermission() {
		List<ContentContext> list = xml_info.getList_content();
		Iterator<ContentContext> itr = list.iterator();
		while (itr.hasNext()) {
			ContentContext ctx = itr.next();
			String rule = ctx.getRuleContext().getText();
			if (rule.contains("uses-permission")) {
				List<ElementContext> list_element = ctx.element();
				Iterator<ElementContext> itr1 = list_element.iterator();
				while (itr1.hasNext()) {
					ElementContext element = itr1.next();
					if (element.getRuleContext().getText()
							.contains("uses-permission")) {
						String attr = element
								.attribute(element.attribute().size() - 1)
								.getRuleContext().getText();
						//System.out.print(attr);
						UsePermission use_permission = new UsePermission();
						use_permission
								.setPermission_name(getUsePermissionName(attr));
						list_use_permission.add(use_permission);
						
					}
				}
			}
		}
	}

	private String getUsePermissionName(String str) {
		// TODO Auto-generated method stub

		if (str.contains("\"")) {
			// int first = name.

			int lastindexofpoint = str.lastIndexOf(".");
			return str.substring(lastindexofpoint + 1, str.length() - 1);
		}
		int lastindexofpoint = str.lastIndexOf(".");
		return str.substring(lastindexofpoint);

	}

}
